package com.wss.lsl.alading.service;

import com.wss.lsl.alading.domain.Card;
import com.wss.lsl.alading.domain.FileItem;
import com.wss.lsl.alading.mapper.FileItemMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FileItemService implements IFileItemService {
    
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private FileItemMapper fileItemMapper;
    
    @Transactional
    @Override
    public void save(FileItem fileItem) {
        fileItemMapper.insert(fileItem);
    }
    
    @Override
    public FileItem findFileItemById(Integer id) {
        return fileItemMapper.select(id);
    }
    
    @Override
    public List<FileItem> searchFileItems(Integer page, Integer rows,
            FileItem fileItem) {
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("startIndex", (page - 1) * rows);
        params.put("pageSize", rows);
        params.put("description", fileItem.getDescription());
        
        return fileItemMapper.selectFileItems(params);
    }
    
    @Override
    public Integer countFileItems(Integer page, Integer rows, FileItem fileItem) {
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("description", fileItem.getDescription());
        
        return fileItemMapper.countFileItems(params);
    }
    
    @Override
    public int deleteFileItemById(Integer id) {
        return fileItemMapper.delete(id);
    }
    
    @Override
    public Integer updateDescContent(FileItem fileItem) {
        
        return fileItemMapper.updateDescContent(fileItem);
    }
    
    @Override
    public Boolean checkDeleteFileItemById(Integer id) {
        
        List<Card> list = fileItemMapper.selectCardByImageId(id);
        if (null != list && list.size() > 0) {
            return false;
        }
        return true;
    }
}
